<?
session_start();
require_once "connect.php";
require_once "functions.php";
$uName = mysql_real_escape_string($_POST['uName']);
$action = mysql_real_escape_string($_POST['action']);

if(isset($_POST['action']) && $action == "SEND"){

	$title = mysql_real_escape_string($_POST['title']);
	$message = mysql_real_escape_string($_POST['message']);
	$toName = mysql_real_escape_string($_POST['toName']);
	$fromId = $_SESSION['userId'];

	
	$output = json_encode(array("status"=>"error", "message"=>"An error occured"));

	if(isset($_SESSION['userId'])){
		$abfrage = "SELECT userId, email, notification FROM ffxivc_users WHERE userName = \"".$toName."\"";
						 
		$res = mysql_query($abfrage);
		$num = mysql_num_rows($res);
		
		if ($res && $num == 1){
			$toId = mysql_result($res, 0, "userId"); 
			$toMail = mysql_result($res, 0, "email"); 
			$notification = mysql_result($res, 0, "notification"); 
			
			$abfrage = "INSERT INTO ffxivc_messages (
								parentId
								,fromId
								,toId
								,title
								,message
								,cdate
								,udate
								 )
							VALUES (
								0
								,\"".$fromId."\"
								,\"".$toId."\"
								,\"".$title."\"
								,\"".$message."\"
								,\"".date('Y-m-d H:i:s')."\"
								,\"".date('Y-m-d H:i:s')."\"
								)";

				$res = mysql_query($abfrage);
				$lastId = mysql_insert_id();
				if ($res){
					$output = json_encode(array("status"=>"success", "message"=>"SENT"));
					/* SEND EMAIL */
					if($notification == 'Y'){
						sendMail($toMail ,$toName ,$_SESSION['userName'] ,$message ,"MSG");
					}
					$abfrage = "INSERT INTO ffxivc_memory ( 
										parentId
										, userId
										, type
										, cdate ) 
										VALUES (
										\"".$lastId."\" 
										,\"".$fromId."\" 
										,'MSG' 
										,\"".date('Y-m-d H:i:s')."\"
										)
										";
					$res = mysql_query($abfrage);
					
				}
		}else{
			$output = json_encode(array("status"=>"error", "message"=>"User not found"));
		}
	}

}else if(isset($_POST['action']) && $action == "REPLY"){

	$pId = mysql_real_escape_string($_POST['pId']);
	$toName = mysql_real_escape_string($_POST['toName']);
	$message = mysql_real_escape_string($_POST['message']);
	$fromId = $_SESSION['userId'];

	
	$output = json_encode(array("status"=>"error", "message"=>"An error occured"));

	if(isset($_SESSION['userId'])){
		$abfrage = "SELECT userId, email, notification FROM ffxivc_users WHERE userName = \"".$toName."\"";
						 
		$res = mysql_query($abfrage);
		$num = mysql_num_rows($res);
		
		if ($res && $num == 1){
			$toId = mysql_result($res, 0, "userId"); 
			$toMail = mysql_result($res, 0, "email"); 
			$notification = mysql_result($res, 0, "notification"); 
			$abfrage = "INSERT INTO ffxivc_messages (
								parentId
								,fromId
								,toId
								,message
								,cdate
								,udate
								 )
							VALUES (
								\"".$pId."\"
								,\"".$fromId."\"
								,\"".$toId."\"
								,\"".$message."\"
								,\"".date('Y-m-d H:i:s')."\"
								,\"".date('Y-m-d H:i:s')."\"
								)";

			$res = mysql_query($abfrage);
			
			if ($res){
				$output = json_encode(array("status"=>"success", "message"=>"SENT"));
				$abfrage = "DELETE FROM ffxivc_memory WHERE parentId = \"".$pId."\" AND type = \"MSG\" ";
				$res = mysql_query($abfrage);
				$abfrage = "UPDATE ffxivc_messages SET udate = \"".date('Y-m-d H:i:s')."\"  WHERE messageId = \"".$pId."\"";
				$res = mysql_query($abfrage);
				
				/* SEND EMAIL */
				if($notification == 'Y'){
					sendMail($toMail ,$toName ,$_SESSION['userName'] ,$message ,"MSG");
				}
				
			}
		}else{
			$output = json_encode(array("status"=>"error", "message"=>"User not found"));
		}
	}
	
}else{

	$output = "
	<script>
		function sendMessage(){
			if($('#title').val() == ''){
				$('#title').addClass('required-field');
			}else if($('#message').val() == ''){
				$('#message').addClass('required-field');
			}else{
				var old = $('#save-submit-button').html();
				$('#save-submit-button').html('<div class=\"center\"><img class=\"login-loader-image\" src=\"media/loader_grey.gif\"></img></div>');
				$.post('php/send_message.php', {title: $('#title').val(), toName: $('#toName').val(), message: $('#message').val(), action: 'SEND'},
					function(data){
						//alert(data.message);
						if(data.status == 'success'){
							showSendMessage(0);
						}else{
							$('#save-submit-button').html(old);
							$('#status_message').html(data.message);
						}
					}
				, 'json');
			}
		}
	</script>

	<div id='send_message_popup_header' class='jqDrag'>
		<div class='fl-left normal-text bold-text blue-text pad-5'>Send message</div>
		 <a class='close' href=\"javascript:showSendMessage(0)\" ><img class='pad-5' src='media/uploadify/cancel.png'/></a>
	</div>
	<div id='send_message_popup_body' class='content'>
		<form class='marg-t-10' method='POST' action='javascript:sendMessage();'>
			<table>
				<tr class='tr-table border-bottom'>
					<td class='td-table vertical-middle' style='width:90px;'><div class='normal-text bold-text pad-l-50 font-10'>User:</div></td>
					<td class='td-table vertical-middle  pad-tb-5' colspan='2'><input id='toName' style='height: 20px;width:200px;' type='text' value='".$uName."'></textarea></td>
				</tr>
				<tr class='tr-table border-bottom'>
					<td class='td-table vertical-middle '><div class='normal-text bold-text pad-l-50 font-10'>Title:</div></td>
					<td class='td-table vertical-middle  pad-tb-5' colspan='2'><input id='title' style='height: 20px;width:200px;' type='text'></textarea></td>
				</tr>
				<tr class='tr-table border-bottom'>
					<td class='td-table vertical-middle'><div class='normal-text bold-text pad-l-50 font-10'>Message:</div></td>
					<td class='td-table vertical-middle pad-tb-5' colspan='2'><textarea id='message' style='width:200px;height:50px;' type='text'></textarea></td>
				</tr>
				<tr class='tr-table'>
					<td class='td-table vertical-middle pad-tb-5 ' colspan='2'><div id='status_message' style='width: 95px;' class='normal-text bold-text error-container'></div></td>
					<td class='td-table vertical-middle horizontal-right' >
						<div id='cancel-submit-button' class='fl-right'><input id='cancel-button' class='cur-point' type='button' value='Cancel' onclick=\"javascript:showSendMessage(0)\"/></div>
						<div id='save-submit-button' class='fl-right'><input id='send-button' class='cur-point' type='submit' value='Send'/></div>
					</td>
				</tr>
			</table>
		</form>
	</div>";

}
echo $output;




?>
